import 'package:animal/constant/font.dart';
import 'package:animal/utils/screen_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

///购物车item
class OrderShopItem extends StatefulWidget {
  late TextEditingController? controller;

  OrderShopItem({Key? key, this.controller}) : super(key: key);

  @override
  State<OrderShopItem> createState() => _OrderShopItemState();
}

class _OrderShopItemState extends State<OrderShopItem> {
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: ScreenUtil().L(15)),
      padding: EdgeInsets.only(bottom: ScreenUtil().L(15)),
      decoration: BoxDecoration(
        border: Border(bottom: BorderSide(width: ScreenUtil().L(1),color: Colors.black26))
      ),
      child: Row(
        children: [
          Image.network("https://cdn.pixabay.com/photo/2016/11/22/07/09/spruce-1848543__340.jpg",
              width: ScreenUtil().L(80), height: ScreenUtil().L(80), fit: BoxFit.cover),
          Container(width: ScreenUtil().L(5)),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  "挺野钓箱全套2019新款渔具多功能钓鱼箱2020款特价箱超轻箱子装备...",
                  style: KFontConstant.myTextStyle(size: 16, color: const Color(0XFF3C3D40)),
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Container(
                        color: Colors.black38,
                        alignment: Alignment.center,
                        margin: EdgeInsets.symmetric(vertical: ScreenUtil().L(10)),
                        padding: EdgeInsets.symmetric(horizontal: ScreenUtil().L(2), vertical: ScreenUtil().L(2)),
                        child: Text("规格名称DGD", style: KFontConstant.myTextStyle(size: 14), textAlign: TextAlign.center)),
                    Row(
                      children: [
                        InkWell(
                          child: Container(
                            padding: EdgeInsets.all(ScreenUtil().L(0.5)),
                            alignment: Alignment.center,
                            decoration: BoxDecoration(border: Border.all(color: Colors.black, width: ScreenUtil().L(1))),
                            child: Image.asset("icons/icon_reduce.png", width: ScreenUtil().L(20), fit: BoxFit.cover),
                          ),
                          onTap: () => numReduce(),
                        ),
                        Container(
                          width: ScreenUtil().L(30),
                          height: ScreenUtil().L(23),
                          alignment: Alignment.center,
                          decoration: BoxDecoration(border: Border.symmetric(horizontal: BorderSide(width: ScreenUtil().L(1), color: Colors.black))),
                          child: TextField(
                            controller: widget.controller!,
                            textAlign: TextAlign.center,
                            decoration: const InputDecoration(
                              border: InputBorder.none,
                              isCollapsed: true,
                            ),
                            keyboardType: TextInputType.number,
                            inputFormatters: [FilteringTextInputFormatter.digitsOnly], //只允许输入数字
                          ),
                        ),
                        InkWell(
                          child: Container(
                            padding: EdgeInsets.all(ScreenUtil().L(0.5)),
                            alignment: Alignment.center,
                            decoration: BoxDecoration(border: Border.all(color: Colors.black, width: ScreenUtil().L(1))),
                            child: Icon(Icons.add, size: ScreenUtil().L(20)),
                          ),
                          onTap: () => numAdd(),
                        ),
                      ],
                    )
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Text("¥1299.85", style: KFontConstant.myTextStyle(size: 14, color: Colors.red)),
                  ],
                ),
              ],
            ),
          )
        ],
      ),
    );
  }

  ///数量增加的方法
  void numAdd() {
    int a = int.parse(widget.controller!.text.trim());
    setState(() {
      ++a;
      widget.controller!.text = a.toString();
    });
  }

  ///数量增加的方法
  void numReduce() {
    int a = int.parse(widget.controller!.text.trim());
    setState(() {
      --a;
      widget.controller!.text = a.toString();
    });
  }
}
